﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;


[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
public class PagerToolStripItem : ToolStripControlHost
{

    /// <summary>
    /// Gets the pager control.
    /// </summary>
    /// <value>The pager control.</value>
    [RefreshProperties(RefreshProperties.All), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public PagerControl PagerControl
    {
        get { return Control as PagerControl; }
    }

    // Constructor ========================================================
    /// <summary>
    /// Initializes a new instance of the <see cref="PagerToolStripItem"/> class.
    /// </summary>
    public PagerToolStripItem()
        : base(new PagerControl())
    {
        (Control as PagerControl).PageSize = 50;
        //
        (Control as PagerControl).PageIndex_Changed += _PageChanged;
    }

    /// <summary>
    /// Retrieves the size of a rectangular area into which a control can be fitted.
    /// </summary>
    /// <param name="constrainingSize">The custom-sized area for a control.</param>
    /// <returns>
    /// An ordered pair of type <see cref="T:System.Drawing.Size"></see> representing the width and height of a rectangle.
    /// </returns>
    /// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
    public override Size GetPreferredSize(Size constrainingSize)
    {
        //return base.GetPreferredSize(constrainingSize);
        //if (this.PagerControl.Width < 380)
        //    this.PagerControl.Width = 380;
        return this.PagerControl.GetPreferredSize(constrainingSize);
    }

    /// <summary>
    /// Subscribes events from the hosted control.
    /// </summary>
    /// <param name="control">The control from which to subscribe events.</param>
    protected override void OnSubscribeControlEvents(Control control)
    {
        base.OnSubscribeControlEvents(control);
        //Add your code here to subsribe to Control Events
    }

    /// <summary>
    /// Unsubscribes events from the hosted control.
    /// </summary>
    /// <param name="control">The control from which to unsubscribe events.</param>
    protected override void OnUnsubscribeControlEvents(Control control)
    {
        base.OnUnsubscribeControlEvents(control);
        //Add your code here to unsubscribe from control events.
    }

    //Wrap event and property to container
    public event PageIndex_ChangedEventHandler PageIndex_Changed;
    public delegate void PageIndex_ChangedEventHandler(int pageIndex);
    //
    private void _PageChanged(int pageIndex)
    {
        if (PageIndex_Changed != null)
        {
            PageIndex_Changed(pageIndex);
        }
    }
    //
    [DefaultValue(50), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public bool AllowChangePageSize
    {
        get { return (Control as PagerControl).AllowChangePageSize; }
        set { (Control as PagerControl).AllowChangePageSize = value; }
    }

    [DefaultValue("Page {0}/{1} ({2} rows)"), Description("Page {0: _PageIndex}/{1 : TotalPage} ({2: _TotalRow} rows)"), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public String CustomPageInfo
    {
        get { return (Control as PagerControl).CustomPageInfo; }
        set { (Control as PagerControl).CustomPageInfo = value; }
    }

    [DefaultValue(50), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public int PageSize
    {
        get { return (Control as PagerControl).PageSize; }
        set { (Control as PagerControl).PageSize = value; }
    }

    [DefaultValue(0), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public int TotalRow
    {
        get { return (Control as PagerControl).TotalRow; }
        set { (Control as PagerControl).TotalRow = value; }
    }

    [DefaultValue(0), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public int PageIndex
    {
        get { return (Control as PagerControl).PageIndex; }
        set { (Control as PagerControl).PageIndex = value; }
    }
    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public int TotalPage
    {
        get { return (Control as PagerControl).TotalPage; }
    }
}
